<template>
  <div>
    <orderheader :title="title">
      <template slot="tips"> </template>
    </orderheader>
    <div class="checkout-main">
      <div class="container">
        <div class="address-detail">
          <div class="address-list">
            <div class="address-header">
              <span class="header-title">收货地址</span>
            </div>
            <ul class="item-li">
              <li
                class="address-item"
                v-for="(item, index) in addressList"
                :key="index"
              >
                <div class="name">{{ item.name }}</div>
                <div class="tel">{{ item.tel }}</div>
                <div class="address-con">
                  {{ item.addressinfo }}
                </div>
                <div class="address-option">
                  <a href="javascript:;">修改</a>
                  <a href="javascript:;" @click="delAddress(item.aid)">删除</a>
                </div>
              </li>
            </ul>
            <ul class="add-ul">
              <li class="address-item">
                <div class="add-desc">
                  <span @click="dialogFormVisible = true">添加新地址</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
        <div class="goods-list">
          <div class="detail-header">产品列表</div>
          <ul class="good-items">
            <li class="good-item">
              <div class="good-img">
                <img
                  src="https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1597890340.54736634.jpg?thumb=1&w=38&h=38"
                />
              </div>
              <div class="good-name">
                <a href="">Redmi 10X 4G 4GB+128GB 明月灰</a>
              </div>
              <div class="price-box">
                <div class="good-price">999元 x 1</div>
                <div class="good-total">999元</div>
              </div>
            </li>
            <li class="good-item">
              <div class="good-img">
                <img
                  src="https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1597890340.54736634.jpg?thumb=1&w=38&h=38"
                />
              </div>
              <div class="good-name">
                <a href="">Redmi 10X 4G 4GB+128GB 明月灰</a>
              </div>
              <div class="price-box">
                <div class="good-price">999元 x 1</div>
                <div class="good-total">999元</div>
              </div>
            </li>
            <li class="good-item">
              <div class="good-img">
                <img
                  src="https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1597890340.54736634.jpg?thumb=1&w=38&h=38"
                />
              </div>
              <div class="good-name">
                <a href="">Redmi 10X 4G 4GB+128GB 明月灰</a>
              </div>
              <div class="price-box">
                <div class="good-price">999元 x 1</div>
                <div class="good-total">999元</div>
              </div>
            </li>
          </ul>
          <div class="bill-money">
            应付总额：<span class="bill-money1">1297</span
            ><span class="bill-money2">元</span>
          </div>
          <div class="footer-detail">
            <div class="footer-con">
              <a href="javascript:;" class="btn-order">立即下单</a>
              <a href="javascript:;" class="btn-cart">返回购物车</a>
            </div>
          </div>
        </div>
      </div>
    </div>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="省市县" :label-width="formLabelWidth">
          <el-cascader
            size="large"
            :options="options_"
            v-model="form.address1"
            placeholder="请选择省市县"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.address2"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

    <navfooter></navfooter>
  </div>
</template>

<script>
import orderheader from "./../components/orderheader";
import navfooter from "./../components/navfooter";
import {
  provinceAndCityData,
  regionData,
  provinceAndCityDataPlus,
  regionDataPlus,
  CodeToText,
  TextToCode,
} from "element-china-area-data";
// import addressbox from "./../components/addressbox";
import qs from "qs";
export default {
  name: "orderconfirm",
  components: {
    orderheader,
    navfooter,
    // addressbox,
  },
  data() {
    return {
      title: "确认订单",
      textarea: "",
      options_: regionData,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        tel: "",
        address1: [],
        address2: "",
      },
      formLabelWidth: "120px",
      addressList: [],
    };
  },
  mounted() {
    this.getAddressList();
  },
  methods: {
    delAddress(aid) {
      var params = {
        aid: aid,
      };
      this.axios.post("/delAddress", qs.stringify(params)).then((res) => {
        this.getAddressList();
      });
    },
    getAddressList() {
      //如果服务端跨域要获取cookie，一定要设置
      this.axios.defaults.withCredentials = true;
      this.axios.get("/getAddressList").then((res) => {
        this.addressList = res;
        console.log(this.addressList)
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var params = {
            name: this.form.name,
            tel: this.form.tel,
            address:
              CodeToText[this.form.address1[0]] +
              CodeToText[this.form.address1[1]] +
              CodeToText[this.form.address1[2]] +
              this.form.address2,
          };
          this.axios.post("/addAddress", qs.stringify(params)).then((res) => {
            this.$refs[formName].resetFields();
            this.dialogFormVisible = false;
            this.getAddressList()
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
.checkout-main {
  background: #f5f5f5;
  padding: 40px 0 100px;
  .container {
    background: #fff;
    .goods-list {
      position: relative;
      padding: 0px 24px 100px;
      .footer-detail {
        padding: 30px 48px;
        border-top: 2px solid #f5f5f5;
        width: 580px;
        height: 40px;
        float: right;
        margin-right: 10px;
        .footer-con {
          .btn-cart {
            color: #b0b0b0;
            border: solid 1px #b0b0b0;
          }
          .btn-order {
            color: #fff;
            background-color: #f25807;
          }
          a {
            display: block;
            float: right;
            margin-left: 30px;
            width: 160px;
            height: 40px;
            text-align: center;
            line-height: 40px;
          }
        }
      }
      .bill-money {
        color: #333;
        font-size: 18px;
        line-height: 40px;
        text-align: right;
        padding-right: 60px;
        border-bottom: 1px solid #e0e0e0;
        .bill-money1 {
          color: #ff6700;
          font-size: 30px;
        }
        .bill-money2 {
          color: #ff6700;
          font-size: 14px;
        }
      }
      .detail-header {
        color: #333;
        font-size: 18px;
        line-height: 40px;
      }
      .good-items {
        padding: 5px 0;
        .good-item {
          position: relative;
          display: flex;
          padding: 15px 0;
          .good-img {
            width: 30px;
            height: 30px;
            margin: 0 10px 0 0;
            img {
              width: 30px;
              height: 30px;
            }
          }
          .good-name {
            width: 650px;
            color: #424242;
            vertical-align: middle;
            line-height: 30px;
          }
          .price-box {
            display: flex;
            width: 440px;
            line-height: 30px;
            .good-price {
              width: 390px;
            }
            .good-total {
              color: #ff6701;
            }
          }
        }
      }
    }
    .address-detail {
      .address-list {
        padding: 48px 24px;
        .address-header {
          margin: 0 0 20px;
          span {
            color: #333;
            font-size: 18px;
            line-height: 20px;
          }
        }
        .add-ul {
          .add-desc {
            width: 100px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            position: relative;
            left: 50%;
            margin-left: -49px;
            top: 50%;
            margin-top: -40px;
            span {
              color: #b0b0b0;
            }
          }
        }
        ul {
          display: inline-block;
          .address-item {
            position: relative;
            padding: 15px 24px 0;
            display: inline-block;
            box-sizing: border-box;
            width: 268px;
            height: 178px;
            border: 1px solid #e0e0e0;
            cursor: pointer;
            position: relative;
            margin-right: 17px;
            margin-bottom: 20px;
            vertical-align: top;
            .name {
              font-size: 18px;
              color: #333;
              line-height: 30px;
              margin-bottom: 10px;
            }
            .tel {
              line-height: 22px;
              color: #757575;
            }
            .address-con {
              width: 220px;
              word-wrap: break-word;
              line-height: 22px;
              color: #757575;
            }
            .address-option {
              text-align: right;
              position: absolute;
              right: 24px;
              bottom: 10px;
            }
            .address-option {
              a {
                display: inline-block;
                margin-left: 10px;
                &:hover {
                  color: #ff6600;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>